package com.example.demo.controller;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.example.demo.model.User;

import jakarta.annotation.Resource;

@RestController
@RequestMapping("/document")
public class DocumentController {

    private final static String INDEX_NAME = "dog_index";
    @Resource
    private RestHighLevelClient restHighLevelClient;

    /**
     * 查询
     */

    @RequestMapping("/save")
    public String save() {

        // 创建对象
        User user = new User() //
            .setId(3) //
            .setUserName("郑军") //
            .setBirthday(LocalDate.of(1999, 8, 15)) //
            .setInfo("墨镜猫咪天下无敌") //
            .setAddress("中国北京市天安门1号") //
            .setSize(1.235645D) //
        ;

        // 创建请求
        IndexRequest indexRequest = new IndexRequest(INDEX_NAME);
        // 规则  put /es_index/_doc/1
        // 【下面这行删除, 会自动生成id】
        indexRequest.id(String.valueOf(user.getId()));
        // 将我们的数据放入请求 json
        indexRequest.source(JSON.toJSONString(user), XContentType.JSON);

        // 客户端发送请求
        IndexResponse index;
        try {
            index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("index = " + index);
        System.out.println("indexStatus = " + index.status());

        return LocalDateTime.now().toString();
    }

    @RequestMapping("/batchSave")
    public String batchSave() {

        // 创建对象
        User user1 = new User() //
            .setId(1) //
            .setUserName("郑军1") //
            .setBirthday(LocalDate.of(1999, 8, 15)) //
            .setInfo("墨镜猫咪天下无敌") //
            .setAddress("窗前明月光，意识地上霜") //
            .setSize(1.235645D) //
        ;
        User user2 = new User() //
            .setId(2) //
            .setUserName("郑军2") //
            .setBirthday(LocalDate.of(1999, 8, 15)) //
            .setInfo("墨镜猫咪天下无敌") //
            .setAddress("剧透望明月，阿弥陀佛") //
            .setSize(1.235645D) //
        ;
        User user3 = new User() //
            .setId(3) //
            .setUserName("郑军3") //
            .setBirthday(LocalDate.of(1999, 8, 15)) //
            .setInfo("墨镜猫咪天下无敌") //
            .setAddress("我隔离期，德玛西亚加息很难喝") //
            .setSize(1.235645D) //
        ;
        User user4 = new User() //
            .setId(4) //
            .setUserName("郑军4") //
            .setBirthday(LocalDate.of(1999, 8, 15)) //
            .setInfo("墨镜猫咪天下无敌") //
            .setAddress("凡人修仙传韩立跑路，周六播放") //
            .setSize(1.235645D) //
        ;
        User user5 = new User() //
            .setId(5) //
            .setUserName("郑军5") //
            .setBirthday(LocalDate.of(1999, 8, 15)) //
            .setInfo("墨镜猫咪天下无敌") //
            .setAddress("火警忍者搞羁绊，这是哦我们热血沸腾的组合键") //
            .setSize(1.235645D) //
        ;
        User user6 = new User() //
            .setId(6) //
            .setUserName("郑军6") //
            .setBirthday(LocalDate.of(1999, 8, 15)) //
            .setInfo("墨镜猫咪天下无敌") //
            .setAddress("电脑由cpu，内存，硬盘，固态硬盘，电源，机箱，显示器等组成") //
            .setSize(1.235645D) //
        ;
        List<User> userList = new ArrayList<>();
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);
        userList.add(user5);
        userList.add(user6);

        // 批处理请求
        BulkRequest bulkRequest = new BulkRequest();
        for (User item : userList) {
            // 如果是批量更新、删除改这里就可以
            IndexRequest request = new IndexRequest(INDEX_NAME) //
                .id(String.valueOf(item.getId()))//
                .source(JSON.toJSONString(item), XContentType.JSON) //
            ;
            bulkRequest.add(request);
        }
        try {
            restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return LocalDateTime.now().toString();

    }

    @RequestMapping("/batchSave2")
    public String batchSave2() {

        // 创建对象
        User user1 = new User() //
            .setId(11) //
            .setUserName("郑军新增1") //
            .setBirthday(LocalDate.of(1999, 8, 15)) //
            .setInfo("墨镜猫咪天下无敌") //
            .setAddress("中国北京市天安门1号") //
            .setSize(1.235645D) //
        ;
        User user2 = new User() //
            .setId(12) //
            .setUserName("郑军新增2") //
            .setBirthday(LocalDate.of(1999, 8, 15)) //
            .setInfo("墨镜猫咪天下无敌") //
            .setAddress("中国北京市天安门1号") //
            .setSize(1.235645D) //
        ;
        User user3 = new User() //
            .setId(3) //
            .setUserName("郑军改3") //
            .setBirthday(LocalDate.of(1999, 8, 15)) //
            .setInfo("墨镜猫咪天下无敌") //
            .setAddress("中国北京市天安门1号") //
            .setSize(1.235645D) //
        ;
        User user4 = new User() //
            .setId(4) //
            .setUserName("郑军改34") //
            .setBirthday(LocalDate.of(1999, 8, 15)) //
            .setInfo("墨镜猫咪天下无敌") //
            .setAddress("中国北京市天安门1号") //
            .setSize(1.235645D) //
        ;
        User user5 = new User() //
            .setId(15) //
            .setUserName("郑军5") //
            .setBirthday(LocalDate.of(1999, 8, 15)) //
            .setInfo("墨镜猫咪天下无敌") //
            .setAddress("中国北京市天安门1号") //
            .setSize(1.235645D) //
        ;
        User user6 = new User() //
            .setId(16) //
            .setUserName("郑军6") //
            .setBirthday(LocalDate.of(1999, 8, 15)) //
            .setInfo("墨镜猫咪天下无敌") //
            .setAddress("中国北京市天安门1号") //
            .setSize(1.235645D) //
        ;
        List<User> saveList = new ArrayList<>();
        saveList.add(user1);
        saveList.add(user2);
        List<User> updateList = new ArrayList<>();
        updateList.add(user3);
        updateList.add(user4);
        List<User> removeList = new ArrayList<>();
        removeList.add(user5);
        removeList.add(user6);

        // 批处理请求
        BulkRequest bulkRequest = new BulkRequest();

        // 批量新增
        for (User item : saveList) {
            IndexRequest request = new IndexRequest(INDEX_NAME) //
                .id(String.valueOf(item.getId())) //
                .source(JSON.toJSONString(item), XContentType.JSON) //
            ;
            bulkRequest.add(request);
        }
        // 批量修改
        for (User item : updateList) {
            UpdateRequest updateRequest = new UpdateRequest(INDEX_NAME, String.valueOf(item.getId()));
            updateRequest.doc(JSON.toJSONString(item), XContentType.JSON);
            bulkRequest.add(updateRequest);
        }
        // 批量删除
        for (User item : removeList) {
            DeleteRequest deleteRequest = new DeleteRequest(INDEX_NAME, String.valueOf(item.getId()));
            bulkRequest.add(deleteRequest);
        }

        try {
            restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return LocalDateTime.now().toString();

    }

    @RequestMapping("/update")
    public String update() {

        // 修改内容
        User user = new User() //
            .setId(3) //
            .setUserName("郑海军33333") //
            .setBirthday(LocalDate.of(1888, 1, 23)) //
            .setInfo("墨镜猫咪天下无222xxxxxx敌") //
            .setAddress("中国北京市天安门12234号") //
            .setSize(1.235645D) //
        ;

        // post /index/_doc/1/_update
        UpdateRequest updateRequest = new UpdateRequest(INDEX_NAME, String.valueOf(user.getId()));
        updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);

        UpdateResponse update;
        try {
            update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("update = " + update);

        return LocalDateTime.now().toString();
    }

    @RequestMapping("/remove")
    public String remove() {

        // DELETE /index/_doc/1
        DeleteRequest deleteRequest = new DeleteRequest(INDEX_NAME, "3");
        DeleteResponse delete;
        try {
            delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("delete.status() = " + delete.status());

        HashMap<String, Object> map = new HashMap<>();
        map.put("time", LocalDateTime.now());

        return JSON.toJSONString(map);
    }

}
